package com.fdb.efp.nls.service.impl;

import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.efp.nls.service.dao.PrdApplyRuleDao;
import com.fdb.efp.nls.service.domain.PrdApplyRule;
import com.fdb.efp.nls.service.facade.PrdApplyRuleService;
import com.fdb.efp.nls.service.vo.PrdApplyRuleVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * <p>ServiceImpl: 产品申请规则关联信息表				<p>
 * @author fuzm
 * @date 2018-06-23
 */
@Service("prdApplyRuleService")
public class PrdApplyRuleServiceImpl implements PrdApplyRuleService, FrameworkService {
	
	private static Logger logger = LoggerFactory.getLogger(PrdApplyRuleServiceImpl.class);

	@Autowired
	private PrdApplyRuleDao prdApplyRuleDao;
	
	/**
	 * 新增操作
	 */
	public int insertPrdApplyRule(PrdApplyRuleVO inPrdApplyRuleVo) {
		logger.debug("当前新增数据为:"+ inPrdApplyRuleVo.toString());
		int num = 0;
		try {
			PrdApplyRule prdApplyRule = new PrdApplyRule();
			beanCopy(inPrdApplyRuleVo, prdApplyRule);
			num = prdApplyRuleDao.insertPrdApplyRule(prdApplyRule);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(PrdApplyRuleVO inPrdApplyRuleVo) {
		logger.debug("当前删除数据条件为:"+ inPrdApplyRuleVo);
		int num = 0;
		try {
			PrdApplyRule prdApplyRule = new PrdApplyRule();
			beanCopy(inPrdApplyRuleVo, prdApplyRule);
			num = prdApplyRuleDao.deleteByPk(prdApplyRule);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inPrdApplyRuleVo +"删除的数据条数为"+ num);
		return num;
	}
	
	/**
	 * 删除申请流水号下所有关联规则信息
	 */
	public int deleteByBizSerno(PrdApplyRuleVO prdApplyRuleVo) {
		logger.debug("当前删除数据条件为:"+ prdApplyRuleVo);
		int num = 0;
		try {
			PrdApplyRule prdApplyRule = new PrdApplyRule();
			beanCopy(prdApplyRuleVo, prdApplyRule);
			num = prdApplyRuleDao.deleteByBizSerno(prdApplyRule);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ prdApplyRuleVo +"删除的数据条数为"+ num);
		return num;
	}

	/**
	 * 更新操作
	 */
	public int updateByPk(PrdApplyRuleVO inPrdApplyRuleVo) {
		logger.debug("当前修改数据为:"+ inPrdApplyRuleVo.toString());
		int num = 0;
		try {
			PrdApplyRule prdApplyRule = new PrdApplyRule();
			beanCopy(inPrdApplyRuleVo, prdApplyRule);
			num = prdApplyRuleDao.updateByPk(prdApplyRule);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inPrdApplyRuleVo +"修改的数据条数为"+ num);
		return num;
	}
	
	/**
	 * 查询操作
	 */
	public PrdApplyRuleVO queryByPk(PrdApplyRuleVO inPrdApplyRuleVo) {
		
		logger.debug("当前查询参数信息为:"+ inPrdApplyRuleVo);
		try {
			PrdApplyRule queryPrdApplyRule = new PrdApplyRule();
			beanCopy(inPrdApplyRuleVo, queryPrdApplyRule);
			PrdApplyRule queryRslPrdApplyRule = prdApplyRuleDao.queryByPk(queryPrdApplyRule);
			if (Objects.nonNull(queryRslPrdApplyRule)) {
				PrdApplyRuleVO outPrdApplyRuleVo = beanCopy(queryRslPrdApplyRule, new PrdApplyRuleVO());
				logger.debug("当前查询结果为:"+ outPrdApplyRuleVo.toString());
				return outPrdApplyRuleVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public List<PrdApplyRuleVO> queryAllByBizSerno(PrdApplyRuleVO prdApplyRuleVo) {
		
		logger.debug("根据流水号查询产品申请规则关联信息的参数信息为:");
		List<PrdApplyRuleVO> list = null;
		try {
			List<PrdApplyRule> prdApplyRules = prdApplyRuleDao.queryAllByBizSerno(prdApplyRuleVo);
			logger.debug("根据流水号查询产品申请规则关联信息的结果集数量为:"+ prdApplyRules.size());
			pageSet(prdApplyRules, prdApplyRuleVo);
			list = (List<PrdApplyRuleVO>) beansCopy(prdApplyRules, PrdApplyRuleVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	}

	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<PrdApplyRuleVO> queryAllOwner(PrdApplyRuleVO prdApplyRuleVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<PrdApplyRuleVO> list = null;
		try {
			List<PrdApplyRule> prdApplyRules = prdApplyRuleDao.queryAllOwnerByPage(prdApplyRuleVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ prdApplyRules.size());
			pageSet(prdApplyRules, prdApplyRuleVo);
			list = (List<PrdApplyRuleVO>) beansCopy(prdApplyRules, PrdApplyRuleVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<PrdApplyRuleVO> queryAllCurrOrg(PrdApplyRuleVO prdApplyRuleVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<PrdApplyRule> prdApplyRules = prdApplyRuleDao.queryAllCurrOrgByPage(prdApplyRuleVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:"+prdApplyRules.size());
		List<PrdApplyRuleVO> list = null;
		try {
			pageSet(prdApplyRules, prdApplyRuleVo);
			list = (List<PrdApplyRuleVO>) beansCopy(prdApplyRules, PrdApplyRuleVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}


	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<PrdApplyRuleVO> queryAllCurrDownOrg(PrdApplyRuleVO prdApplyRuleVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<PrdApplyRule> prdApplyRules = prdApplyRuleDao.queryAllCurrDownOrgByPage(prdApplyRuleVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:"+ prdApplyRules.size());
		List<PrdApplyRuleVO> list = null;
		try {
			pageSet(prdApplyRules, prdApplyRuleVo);
			list = (List<PrdApplyRuleVO>) beansCopy(prdApplyRules, PrdApplyRuleVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}
}
